package com.quik.web.action.overview;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.backend.service.billing.BillingService;
import com.quik.common.Constants;
import com.quik.common.SysSystemCodeUtils;
import com.quik.entity.BizBilling;
import com.quik.entity.BizBillingItem;
import com.quik.entity.BizBillingService;
import com.quik.entity.DtaProduct;
import com.quik.entity.DtaProductCategory;
import com.quik.entity.DtaProductSubCategory;
import com.quik.entity.DtaToken;
import com.quik.entity.DtaTokenType;
import com.quik.entity.DtaWorker;
import com.quik.entity.DtaWorkerType;
import com.quik.entity.Statistics;
import com.quik.entity.SysAccount;
import com.quik.entity.SysSystemCode;
import com.quik.web.action.BaseAction;
import com.quik.web.action.vo.JsonSuccessResult;

@Controller
@RequestMapping("/tokenOverview")
public class TokenOverviewAction extends BaseAction {
	private static Logger log = (Logger) Logger
			.getLogger(TokenOverviewAction.class);

	@Autowired
	private BillingService billingService;

	@RequestMapping(value = "submitBillingServiceToUpdate", method = RequestMethod.POST)
	@ResponseBody
	public Object submitBillingServiceToUpdate(
			BizBillingService bizBillingService, HttpServletRequest request) {
		try {
			SysAccount account = this.getLoginAccount(request);
			JsonSuccessResult rs = new JsonSuccessResult();
			billingService.updateBillingServiceIfNotNull(bizBillingService,
					account);
			rs.add("name", "rya");
			return rs;
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processJsonOnException(e);
		}
	}

	@RequestMapping(value = "submitBillingServiceToChangeWorker", method = RequestMethod.POST)
	@ResponseBody
	public Object submitBillingServiceToChangeWorker(
			BizBillingService bizBillingService, HttpServletRequest request) {
		try {
			SysAccount account = this.getLoginAccount(request);
			JsonSuccessResult rs = new JsonSuccessResult();
			billingService.updateBillingServiceIfNotNull(bizBillingService,
					account);
			rs.add("name", "rya");
			return rs;
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processJsonOnException(e);
		}
	}

	@RequestMapping(value = "submitBillingItemToKickoff", method = RequestMethod.POST)
	@ResponseBody
	public Object submitBillingItemToKickoff(BizBillingItem bizBillingItem,
			HttpServletRequest request) {
		try {
			// SysAccount account =
			this.getLoginAccount(request);
			JsonSuccessResult rs = new JsonSuccessResult();
			return rs;
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processJsonOnException(e);
		}
	}

	@RequestMapping(value = "submitBillingItemToEnd", method = RequestMethod.POST)
	@ResponseBody
	public Object submitBillingItemToEnd(Integer id, String tokenNo,
			HttpServletRequest request) {
		try {
			SysAccount account = this.getLoginAccount(request);
			JsonSuccessResult rs = new JsonSuccessResult();
			this.billingService.updateBillingItemForEnd(id, account);
			BizBilling billing = this.billingService
					.findBillingByToken(tokenNo);
			rs.add("billing", billing);
			List<BizBillingItem> billingItems = this.billingService
					.findBillingItemByBillingId(billing.getId().toString());
			rs.add("billingItems", billingItems);
			return rs;
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processJsonOnException(e);
		}
	}

	@RequestMapping(value = "submitBillingItemToCancel", method = RequestMethod.POST)
	@ResponseBody
	public Object submitBillingItemToCancel(Integer id, String staffNo,
			String password, String tokenNo, HttpServletRequest request) {
		try {
			SysAccount account = this.getLoginAccount(request);
			JsonSuccessResult rs = new JsonSuccessResult();
			this.billingService.updateBillingItemForCancel(id, staffNo,
					password, account);
			BizBilling billing = this.billingService
					.findBillingByToken(tokenNo);
			rs.add("billing", billing);
			List<BizBillingItem> billingItems = this.billingService
					.findBillingItemByBillingId(billing.getId().toString());
			rs.add("billingItems", billingItems);
			return rs;
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processJsonOnException(e);
		}
	}

	@RequestMapping(value = "submitTokenToOpen", method = RequestMethod.POST)
	public ModelAndView submitTokenToOpen(String[] tokenNos,
			String contactStaffNo, HttpServletRequest request) {
		Integer id = 0;
		String tokenNo = null;
		try {
			SysAccount account = this.getLoginAccount(request);
			BizBilling billing = this.billingService.submitTokenToOpen(
					tokenNos, contactStaffNo, account);
			id = billing.getId();
			if (tokenNos != null && tokenNos.length > 0) {
				tokenNo = tokenNos[0];
			}
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processModelAndViewOnException(e);
		}

		return new ModelAndView("redirect:/tokenOverview/main?billingId=" + id
				+ "&tokenNo=" + (tokenNo == null ? "" : tokenNo));
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "main", method = RequestMethod.GET)
	public ModelAndView main(String billingId, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/tokenOverview/main");

		try {
			this.getLoginAccount(request);
			List<DtaToken> tokenList = billingService.queryByMap(
					DtaToken.class, null);
			List<DtaTokenType> tokenTypeList = this.billingService.queryByMap(
					DtaTokenType.class, null);
			List<SysSystemCode> tokenStatusList = SysSystemCodeUtils
					.getTokenStatusList();
			// 统计
			List<Statistics> tokenStatList = this.billingService.load(
					"findTokenStatList", "[dta_token]_tokenStatus");

			List<DtaProduct> productList = this.billingService.queryByMap(
					DtaProduct.class, null);
			List<DtaProductCategory> productCategoryList = this.billingService
					.queryByMap(DtaProductCategory.class, null);
			List<DtaProductSubCategory> productSubCategoryList = this.billingService
					.queryByMap(DtaProductSubCategory.class, null);
			if (!StringUtils.isEmpty(billingId)) {
				BizBilling billing = (BizBilling) this.billingService
						.queryById(BizBilling.class, new Integer(billingId));
				mav.addObject("billing", billing);
				List<BizBillingItem> billingItems = this.billingService.load(
						"queryBillingItemByBillingIdWithGroup", new Integer(
								billingId));
				mav.addObject("billingItems", billingItems);
			}

			List<DtaWorkerType> workerTypes = billingService.queryByMap(
					DtaWorkerType.class, null);
			List<DtaWorker> workers = billingService.queryByMap(
					DtaWorker.class, null);
			List<SysSystemCode> callTurns = SysSystemCodeUtils
					.getCallTurnList();

			mav.addObject("callTurns", callTurns);
			mav.addObject("workers", workers);
			mav.addObject("workerTypes", workerTypes);
			mav.addObject("tokenList", tokenList);
			mav.addObject("tokenTypeList", tokenTypeList);
			mav.addObject("tokenStatusList", tokenStatusList);
			mav.addObject("tokenStatList", tokenStatList);
			mav.addObject("productList", productList);
			mav.addObject("productCategoryList", productCategoryList);
			mav.addObject("productSubCategoryList", productSubCategoryList);
		} catch (Exception e) {
			log.error("Error : " + e, e);
			return this.processModelAndViewOnException(e);
		}

		return mav;
	}

	@RequestMapping(value = "submitBillingItemForKickoff", method = RequestMethod.POST)
	public ModelAndView submitBillingItemForKickoff(Integer billingId,
			Integer billingItemId, String tokenNo, Integer[] serviceIds,
			String[] workerNos, String[] turnTypes, String[] callTurnWorkerNos,
			HttpServletRequest request) {
		SysAccount account = (SysAccount) request.getSession().getAttribute(
				Constants.SESSION_ACCOUNT);
		try {
			billingService.submitBillingItemForKickoff(billingItemId, tokenNo,
					serviceIds, workerNos, turnTypes, callTurnWorkerNos,
					account);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return new ModelAndView("redirect:/tokenOverview/main?billingId="
				+ billingId + "&tokenNo=" + tokenNo);
	}

	@RequestMapping(value = "submitProductToBilling", method = RequestMethod.POST)
	public ModelAndView submitProductToBilling(Integer billingId,
			String[] productNos, Integer[] qtys, String roomNo, String tokenNo,
			HttpServletRequest request) {
		SysAccount account = (SysAccount) request.getSession().getAttribute(
				Constants.SESSION_ACCOUNT);
		try {

			billingId = billingService.submitProductToBilling(billingId,
					productNos, qtys, roomNo, tokenNo, account.getStaff_no());

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return new ModelAndView("redirect:/tokenOverview/main?billingId="
				+ billingId + "&tokenNo=" + tokenNo);
	}

	@RequestMapping(value = "queryBillingByTokenNo", method = RequestMethod.POST)
	@ResponseBody
	public Object queryBillingByTokenNo(String tokenNo) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(tokenNo)) {
			BizBilling billing = this.billingService
					.findBillingByToken(tokenNo);
			data.put("billing", billing);
			List<BizBillingItem> billingItems = this.billingService
					.findBillingItemByBillingId(billing.getId().toString());
			data.put("billingItems", billingItems);
		}
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("state", "success");
		rs.put("data", data);
		return rs;
	}

}
